package org.example.trans;
import java.util.*;
import org.example.model.*;

/**
 * @author lenovo
 */

public class TransSort {
    //将HashMap<String,Course>转为ArrayList<ArrayList<String>>的第一行
    //将ArrayList<Score>转为ArrayList<ArrayList<String>>从第二行开始
    public static ArrayList<ArrayList<String>> transSortWrite
            (HashMap<String,Course> hashMapCourse,ArrayList<Score> arrayListScore){
        ArrayList<ArrayList<String>> arrayListOut = new ArrayList<>();
        if(hashMapCourse == null || arrayListScore == null){
            return null;
        }
        arrayListOut.add(new ArrayList<>());
        arrayListOut.get(0).add("学号");
        arrayListOut.get(0).add("姓名");
        arrayListOut.get(0).add("性别");
        arrayListOut.get(0).add("出生日期");
        for(Map.Entry<String,Course> map : hashMapCourse.entrySet()){
            arrayListOut.get(0).add(map.getKey());
        }
        for(Score score : arrayListScore){
            ArrayList<String> arrayListIn = new ArrayList<>();
            arrayListIn.add(score.getStudent().getNum());
            arrayListIn.add(score.getStudent().getName());
            arrayListIn.add(score.getStudent().getGender());
            arrayListIn.add(score.getStudent().getBirthday());
            for(int i = 4 ; i < arrayListOut.get(0).size() ; i++){
                if(score.getScore().containsKey(arrayListOut.get(0).get(i))){
                    arrayListIn.add(String.format("%.2f",score.getScore().get(
                            arrayListOut.get(0).get(i)).getScore()));
                }else{
                    arrayListIn.add("--");
                }
            }
            arrayListOut.add(arrayListIn);
        }
        return arrayListOut;
    }
    //将HashMap<String,Course>转为ArrayList<ArrayList<String>>的第一行
    //将ArrayList<Score>转为ArrayList<ArrayList<String>>从第二行开始
    //将Score转为ArrayList<ArrayList<String>>最后部分，前加一空行
    public static ArrayList<ArrayList<String>> transSortWriteTotal
            (HashMap<String,Course> hashMapCourse,ArrayList<Score> arrayListScore,
             Score averageCoursescore){
        ArrayList<ArrayList<String>> arrayListOut = new ArrayList<>();
        if(hashMapCourse == null || arrayListScore == null){
            return null;
        }
        arrayListOut.add(new ArrayList<>());
        arrayListOut.get(0).add("学号");
        arrayListOut.get(0).add("姓名");
        arrayListOut.get(0).add("性别");
        arrayListOut.get(0).add("出生日期");
        for(Map.Entry<String,Course> map : hashMapCourse.entrySet()){
            arrayListOut.get(0).add(map.getKey());
        }
        arrayListOut.get(0).add("平均成绩");
        for(Score score : arrayListScore){
            ArrayList<String> arrayListIn = new ArrayList<>();
            arrayListIn.add(score.getStudent().getNum());
            arrayListIn.add(score.getStudent().getName());
            arrayListIn.add(score.getStudent().getGender());
            arrayListIn.add(score.getStudent().getBirthday());
            for(int i = 4 ; i < arrayListOut.get(0).size()-1 ; i++){
                if(score.getScore().containsKey(arrayListOut.get(0).get(i))){
                    arrayListIn.add(String.format("%.2f",score.getScore().get(
                            arrayListOut.get(0).get(i)).getScore()));
                }else{
                    arrayListIn.add("--");
                }
            }
            if(!score.getScore().isEmpty()){
                arrayListIn.add(String.format("%.2f",score.getAverage()));
            }else{
                arrayListIn.add("--");
            }
            arrayListOut.add(arrayListIn);
        }
        arrayListOut.add(new ArrayList<>());
        for(Map.Entry<String,Course> map : hashMapCourse.entrySet()){
            ArrayList<String> arrayListIn = new ArrayList<>();
            arrayListIn.add(map.getKey() + "平均成绩：");
            if(averageCoursescore.getScore().containsKey(map.getKey())){
                arrayListIn.add(String.format("%.2f",averageCoursescore.getScore().
                        get(map.getKey()).getScore()));
            }else{
                arrayListIn.add("--");
            }
            arrayListOut.add(arrayListIn);
        }
        ArrayList<String> arrayListIn2 = new ArrayList<>();
        arrayListIn2.add("总平均成绩：");
        if (!averageCoursescore.getScore().isEmpty()) {
            arrayListIn2.add(String.format("%.2f", averageCoursescore.getAverage()));
        }else{
            arrayListIn2.add("--");
        }
        arrayListOut.add(arrayListIn2);
        return arrayListOut;
    }
}
